package com.larry.tinyweb.plugin.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * Created by larry on 16/12/11.
 * @author larry
 */

public class TransactionInvocationHandler implements InvocationHandler {
    private Object proxyTarget;
    private Transaction transaction;

    public TransactionInvocationHandler(Object proxyTarget, Transaction transaction) {
        this.proxyTarget = proxyTarget;
        this.transaction = transaction;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        transaction.start();

        Object result = method.invoke(proxyTarget, args);

        transaction.stop();

        return result;
    }
}